package com.xihespace.xihecommonlibrary.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@SuppressWarnings("ALL")
public class GridChartView extends View {
    private String TAG = GridChartView.class.getSimpleName();
    private int mWidth;
    private int mHeight;
    private final int[] mCoordinatesY = {40, 60, 80, 85, 90, 100, 110, 115};
    private final int[] mCoordinatesX = {40, 90, 120, 130, 140, 160, 180, 190};
    private final ArrayList<Float> mXCoors = new ArrayList<>();
    private final ArrayList<Float> mYCoors = new ArrayList<>();
    private final String[] bloodPressureRange = {"低血压", "理想血压", "正常", "正常偏高血压", "轻度高血压", "中度高血压", "高度高血压"};
    //颜色
    private String[] mColormCoordinates = {"#5D4EF1", "#35C2BF", "#179EF3", "#BDB14E", "#BD927F", "#BA7A82", "#B06970"};
    private Paint mLinePaint = new Paint();
    private Paint mPathPaint = new Paint();
    private Paint mTextPaint = new Paint();
    private Paint mCoorTextPaint = new Paint();
    //左边的宽度
    private int mLeft;
    //右边的宽度
    private int mRight;
    private int mTop;
    private int mBottom;
    private float mFactorX;
    private float mFactorY;
    //X轴的长度
    private float mCoorX;
    //Y轴的长度
    private float mCoorY;

    public GridChartView(Context context) {
        super(context);
        init();
    }

    public GridChartView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GridChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获得自定义控件的高度和宽度
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeight = MeasureSpec.getSize(heightMeasureSpec);
        //设置宽高
        setMeasuredDimension(mWidth, mHeight);
    }

    /**
     * 初始化
     */
    private void init() {
        mLinePaint.setColor(Color.WHITE);
        mPathPaint.setColor(Color.WHITE);
        mPathPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setTextSize(40);
        mTextPaint.setColor(Color.WHITE);
        mCoorTextPaint.setTextSize(30);
        mCoorTextPaint.setColor(Color.BLACK);
        mCoorTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        mCoorTextPaint.setStyle(Paint.Style.STROKE);
        mLeft = DensityUtil.dp2px(getContext(), 50);
        mRight = DensityUtil.dp2px(getContext(), 50);
        mTop = DensityUtil.dp2px(getContext(), 50);
        mBottom = DensityUtil.dp2px(getContext(), 50);
    }

    private float[] floatCoordinates = new float[4];

    @Override
    protected void onDraw(Canvas canvas) {
        //1.计算坐标系的搞和宽  求取原点的坐标
        float mOriginX = mLeft;
        float mOriginY = mHeight - mBottom;
        //求取X轴的坐标
        float mAxleXx = mWidth - mRight;
        float mAxleXy = mOriginY;
        //求取Y轴的坐标
        float mAxleYx = mOriginX;
        float mAxleYy = mTop;
        //求取系数
        mCoorX = mWidth - mLeft - mRight;
        mFactorX = mCoorX / (mCoordinatesX[mCoordinatesX.length - 1] - mCoordinatesX[0]);
        mCoorY = mHeight - mTop - mBottom;
        mFactorY = mCoorY / (mCoordinatesY[mCoordinatesY.length - 1] - mCoordinatesY[0]);
        //绘制坐标系
        setFloatCoordinates(mOriginX, mOriginY, mAxleXx, mAxleXy);
        canvas.drawLines(floatCoordinates, mLinePaint);
        setFloatCoordinates(mOriginX, mOriginY, mAxleYx, mAxleYy);
        canvas.drawLines(floatCoordinates, mLinePaint);
        //绘制坐标轴的刻度
        Path[] paths = getPath(mOriginX, mOriginY);
        int length = paths.length;
        mColormCoordinates = reverse(mColormCoordinates);
        for (int i = 0; i < length; i++) {
            mPathPaint.setColor(Color.parseColor(mColormCoordinates[i]));
            canvas.drawPath(paths[i], mPathPaint);
        }
        //绘制文字
        int size = mYCoors.size();
        Paint.FontMetrics metrics = mTextPaint.getFontMetrics();
        Paint.FontMetrics fontMetrics = mCoorTextPaint.getFontMetrics();
        for (int i = 1; i < size; i++) {
            canvas.drawText(bloodPressureRange[i - 1], mOriginX + 10, mYCoors.get(i) - metrics.top, mTextPaint);
        }
        Log.e(TAG, fontMetrics.ascent + "====" + fontMetrics.top);
        for (int i = 0; i < size; i++) {
            canvas.drawText(mCoordinatesY[i] + "", mOriginX + (float) (fontMetrics.ascent * (i <= 4 ? 1.5 : 2)), mYCoors.get(i) - (float) (metrics.top / (i == 0 ? 3 : 2.5)), mCoorTextPaint);
            canvas.drawText(mCoordinatesX[i] + "", mXCoors.get(i) + fontMetrics.ascent / 2, mOriginY - (float) (metrics.top / 1.5), mCoorTextPaint);
        }
    }

    /**
     * 数组反转
     *
     * @param revers
     * @param <T>
     * @return
     */
    private <T> T[] reverse(T[] revers) {
        List<T> asList = Arrays.asList(revers);
        Collections.reverse(asList);
        revers = asList.toArray(revers);
        return revers;
    }

    //获取分割线的Path
    private Path[] getPath(float mOriginX, float mOriginY) {
        int length = mCoordinatesY.length;
        mXCoors.add(mOriginX);
        mYCoors.add(mOriginY);
        Path[] paths = new Path[length - 1];
        for (int i = 0; i < length - 1; i++) {
            Path path = new Path();
            float y = mCoorY - (mCoordinatesY[i + 1] - mCoordinatesY[0]) * mFactorY + mTop;
            path.moveTo(mOriginX, y);
            float x = (mCoordinatesX[i + 1] - mCoordinatesX[0]) * mFactorX + mLeft;
            path.lineTo(x, y);
            path.lineTo(x, mOriginY);
            path.lineTo(mOriginX, mOriginY);
            path.close();
            paths[length - 2 - i] = path;
            mXCoors.add(x);
            mYCoors.add(y);
        }
        return paths;
    }

    /**
     * 赋值操作
     *
     * @param floatCoordinates
     */
    private void setFloatCoordinates(float... floatCoordinates) {
        if (floatCoordinates != null && floatCoordinates.length == 4) {
            System.arraycopy(floatCoordinates, 0, this.floatCoordinates, 0, floatCoordinates.length);
        } else {
            throw new IllegalArgumentException("参数异常");
        }
    }
}
